package dao

import (
	"context"

	"gorm.io/gorm/clause"

	"open/internal/model"
)

// CreateSchedule 创建调度任务
func (d *Dao) CreateSchedule(ctx context.Context, schedule *model.Schedule) error {
	return d.FromContext(ctx).Create(schedule).Error
}

// LockSchedule 锁定任务
func (d *Dao) LockSchedule(ctx context.Context, id int64) (*model.Schedule, error) {
	schedule := new(model.Schedule)
	err := d.FromContext(ctx).Clauses(clause.Locking{Strength: "UPDATE"}).First(schedule, id).Error
	return schedule, err
}

// SaveSchedule 保存调度任务
func (d *Dao) SaveSchedule(ctx context.Context, schedule *model.Schedule) error {
	return d.FromContext(ctx).Save(schedule).Error
}
